
package com.tt.dao;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.tt.entity.ListImg;
import com.tt.entity.Page;
import com.tt.entity.Strategy;
import com.tt.entity.User;

@Repository
@Transactional
public class StrategyDaoImpl {
	@Resource
	private SessionFactory sessionFactory;
	//发布动态
	public int addStrategy(Strategy strategy,Integer uid,Set<ListImg> listImgs){
		User user = this.sessionFactory.getCurrentSession().load(User.class, uid);
		List<ListImg> listimg = new ArrayList<ListImg>();
		for(ListImg s:listImgs){
			this.sessionFactory.getCurrentSession().save(s);
			s.setStrategy(strategy);
			listimg.add(s);
		}
		strategy.setListimg(listimg);
		Set<Strategy> strategys = user.getStrategys();
		strategys.add(strategy);
		user.setStrategys(strategys);
		strategy.setUser(user);
		this.sessionFactory.getCurrentSession().save(user);
		int save = (int) this.sessionFactory.getCurrentSession().save(strategy);
		return save;
	}
	//删除动态
	public void deleteStrategy(int id){
		Strategy strategy = this.sessionFactory.getCurrentSession().load(Strategy.class, id);
		this.sessionFactory.getCurrentSession().delete(strategy);	
	}
	//查看动态
	public Strategy findStraegyById(int id){
		Strategy strategy = this.sessionFactory.getCurrentSession().load(Strategy.class, id);
		return strategy;
	}
	//修改动态
	public void updateStrategy(Strategy strategy){
		this.sessionFactory.getCurrentSession().clear();
		this.sessionFactory.getCurrentSession().update(strategy);
	}
	//根据id查询图片列表
	public List<ListImg> findStrategyListImgById(int id){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from ListImg limg where limg.strategy.id=?");
		query.setParameter(0, id);
		List<ListImg> list = query.list();
		return list;
	}
	//删除图片列表
	public void deleteListImg(List<ListImg> listImgs){
		for(ListImg lImg:listImgs){
			this.sessionFactory.getCurrentSession().delete(lImg);
		}
	}
	//查询全部动态
	public Page<Strategy> findAllStrategy(int pageNum,int pageSize){
		String hql="from Strategy";
		String hql1="select count(id) from Strategy";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		Query query1 = this.sessionFactory.getCurrentSession().createQuery(hql1);
		Number number = (Number) query1.uniqueResult();
		int count=number.intValue();
		System.out.println(count);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Strategy> list = query.list();
		Page<Strategy> page=new Page<>();
		page.setList(list);
		page.setPageSize(pageSize);
		page.setPageNum(pageNum);
		page.setTotalCount(count);
		return page;
	}
	//根据用户id查询该用户所发布的所有动态
	public Page<Strategy> findUserStrategyById(Integer uid,int pageSize,int pageNum){
		Query query = this.sessionFactory.getCurrentSession().createQuery("from Strategy s where s.user.id=?");
		Query query2 = this.sessionFactory.getCurrentSession().createQuery("select count(id) from Strategy s where s.user.id=?");
		query2.setParameter(0, uid);
		Number number = (Number) query2.uniqueResult();
		int count=number.intValue();
		System.out.println(count);
		query.setParameter(0, uid);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Strategy> list = query.list();
		Page<Strategy> page=new Page<>(pageNum, pageSize);
		page.setList(list);
		page.setTotalCount(count);
		return page;
	}
	//根据关键词查询主题或地址对应的动态
	public Page<Strategy> searchStrategy(String search,int pageNum,int pageSize){
		/*Criteria ce=this.sessionFactory.getCurrentSession().createCriteria(Strategy.class);
		ce.add(Restrictions.or(Restrictions.like("themes", "%"+search+"%"), Restrictions.like("address", "%"+search+"%")));
		ce.setFirstResult((pageNum-1)*pageSize);
		ce.setMaxResults(pageSize);
		List<Strategy> list = ce.list();*/
		String hql="select count(id) from Strategy where themes like '%"+search+"%' or address like '%"+search+"%'";
		String hql2="from Strategy where themes like '%"+search+"%' or address like '%"+search+"%'";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		Query query2 = this.sessionFactory.getCurrentSession().createQuery(hql2);
		Number number = (Number) query.uniqueResult();
		int count = number.intValue();
		System.out.println("总量："+count);
		query2.setFirstResult((pageNum-1)*pageSize);
		query2.setMaxResults(pageSize);
		List<Strategy> list = query2.list();
		Page<Strategy> page=new Page<>(pageNum, pageSize);
		page.setTotalCount(count);
		page.setList(list);
		return page;
	}
	//根据地址查询动态
	public Page<Strategy> classificationStrategy(int pageNum,int pageSize,String address){
		String hql="select count(id) from Strategy where address=?";
		String hql2="from Strategy where address=?";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		Query query2 = this.sessionFactory.getCurrentSession().createQuery(hql2);
		query.setParameter(0, address);
		query2.setParameter(0, address);
		query2.setFirstResult((pageNum-1)*pageSize);
		query2.setMaxResults(pageSize);
		Number number = (Number) query.uniqueResult();
		int count=number.intValue();
		System.out.println("总量："+count);
		List<Strategy> list = query2.list();
		Page<Strategy> page=new Page<>(pageNum, pageSize);
		page.setTotalCount(count);
		page.setList(list);
		return page;
	}
	//根据id查询用户
	public User findUserById(int id){
		User user = this.sessionFactory.getCurrentSession().load(User.class, id);
		return user;
	}
}
